<template>
  <basic-container>
    <avue-crud 
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :search.sync="search"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button 
          type="danger"
          size="small"
          icon="el-icon-delete"
          v-if="permission.tenant_delete"
          plain
          @click="handleDelete"
        >删 除</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {mapGetters} from "vuex";
  import {getList, getDetail, remove, update, add} from "@/api/system/tenant";
  import {getDictionaryAPI} from '@/api/system/dict'
  
  export default {
    data(){
      return {
        form: {},
        data: [],
        selectionList: [],
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 5,
          searchGutter: 10,
          border: true,
          index: true,
          selection: true,
          viewBtn: true,
          dialogClickModal: false,
          searchLabelPosition: 'top',
          searchIcon: true,
          searchIndex: 3,
          column: [
            {
              label: "租户号",
              prop: "code",
              width: 100,
              search: true,
              searchLabel: '',
              searchPlaceholder: '租户号',
              span: 24,
              rules: [{
                required: true,
                message: "请输入租户号",
                trigger: "blur"
              }]
            },
            {
              label: "租户名称",
              prop: "tenantName",
              search: true,
              searchLabel: '',
              searchPlaceholder: '租户名称',
              span: 24,
              rules: [{
                required: true,
                message: "请输入参数名称",
                trigger: "blur"
              }]
            },
            {
              label: "联系人",
              prop: "linkman",
              search: true,
              searchLabel: '',
              searchPlaceholder: '联系人',
              rules: [{
                required: true,
                message: "请输入联系人",
                trigger: "blur"
              }]
            },
            {
              label: "联系电话",
              prop: "phone",
              width: 100,
            },
            {
              label: "联系地址",
              prop: "address",
              span: 24,
              minRows: 2,
              type: "textarea",
              hide: true,
            },
            {
              label: "过期时间",
              prop: "expireTime",
              type: "datetime",
              format: "yyyy-MM-dd hh:mm:ss",
              valueFormat: "yyyy-MM-dd hh:mm:ss",
              defaultTime: "00:00:00",
              width: 130,
            },
            {
              label: "状态",
              prop: "status",
              type: "select",
              dicUrl: getDictionaryAPI +'?code=tenant_status',
              dataType: 'number',
              props:{
                label: 'dictText',
                value: 'dictValue'
              },
              width: 50,
              rules: [{
                required: true,
                message: "请选择状态",
                trigger: "blur"
              }]
            }
          ]
        }
      }
    },
    computed: {
      ...mapGetters(["userInfo", "permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.tenant_add, false),
          viewBtn: this.vaildData(this.permission.tenant_view, false),
          delBtn: this.vaildData(this.permission.tenant_delete, false),
          editBtn: this.vaildData(this.permission.tenant_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      async rowSave(row, done, loading) {
        try {
          var { data } = await add(row)
        } finally {
          loading();
        }
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        row.id = data
        done(row);
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row, index, done) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            // 数据回调进行刷新
            done(row);
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      async beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          const { data } = await getDetail(this.form.id)
          this.form = data;
        }
        done();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      async onLoad(page, params = {}) {
        this.loading = true;
        const { data } = await getList(page.currentPage, page.pageSize, Object.assign(params, this.query))
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      },
    }
  }
</script>

<style>
</style>
